package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ICustomerTransferService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customerTransfer")
public class CustomerTransferController {

    @Autowired
    private ICustomerTransferService customerTransferService;
    @Autowired
    private CustomerMapper customerMapper;

    //查询所有
    @RequestMapping("/list")
    @RequiresPermissions(value = {"customerTransfer:list", "移交客户列表"},logical = Logical.OR)
    public String list(Model model,@ModelAttribute("qo")QueryObject qo){

        PageInfo pageInfo = customerTransferService.query(qo);
        model.addAttribute("result",pageInfo);
        return "/customerTransfer/list";
    }



    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"customerTransfer:saveOrupdate", "移交客户新增/编辑"},logical = Logical.OR)
    public JsonResult saveOrupdate(CustomerTransfer customerTransfer){
        Integer status = customerTransfer.getCustomer().getStatus();
        if(status == 4){
           customerMapper.updateStatus(0,customerTransfer.getCustomer().getId());
        }
        try {
            if (customerTransfer.getId() == null) {
                customerTransferService.save(customerTransfer);
            }else{
                customerTransferService.updateByPrimaryKey(customerTransfer);
            }
            return new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false," 操作失败,请重新操作!");
        }
    }

    //客户吸纳
    @RequestMapping("/absorb")
    @ResponseBody
    @RequiresPermissions(value = {"customerTransfer:absorb", "吸纳客户"},logical = Logical.OR)
    public JsonResult absorb(CustomerTransfer customerTransfer){
        //修改客户的状态:改为潜在客户
        customerMapper.updateStatus(0,customerTransfer.getCustomer().getId());
        customerTransferService.save(customerTransfer);
        return new JsonResult();
    }
}
